import React, { Component } from 'react';
import {
    Image,
    View,
    Text,
    Dimensions,
    StyleSheet,
    AppRegistry,
    Platform,
    ToastAndroid,
    BackHandler,
    WebView,
    Alert,
    Modal
} from 'react-native';
import { Container, Content, Spinner, List, ListItem, Toast, Body } from 'native-base';
const { width, height } = Dimensions.get('window');
import { connect } from 'react-redux';
import URL from '../../net/url'
import Set_Net from '../../net/setting'
import db_Cache from '../../db/student_table'
export default class sett extends Component {
    constructor(props) {
        super(props);
        this.state = {
            wait_state: false
        }
    }

    //清空缓存
    clearCache_tishi() {
        Alert.alert(
            '',
            '确定清空缓存？',
            [
                { text: '取消', onPress: () => console.log('Cancel Pressed!') },
                { text: '确定', onPress: () => this.clearCache() },
            ]
        );

    }
    clearCache() {
        this.setState({ wait_state: true });
        let dbCache = new db_Cache();
        dbCache.deleteStudentInfo();
        this.setState({ wait_state: false });
        ToastAndroid.show('清理完毕！', ToastAndroid.SHORT);
    }
    //退出应用
    exitApp() {
        Alert.alert(
            '',
            '确定退出应用？',
            [
                { text: '取消', onPress: () => console.log('Cancel Pressed!') },
                {
                    text: '确定', onPress: () => {
                        let dbCache = new db_Cache();
                        dbCache.deleteStudentInfo(); BackHandler.exitApp();
                    }
                },
            ]
        );

    }
    //检查更新
    async checkNewVersion() {
        if(Platform.OS=='ios'){
             Alert.alert("温馨提示", "苹果用户请到Appstore中更新");    
        }else{
            let setNet = new Set_Net();
        try {
            this.setState({ wait_state: true });
            var nowVersion = '1.2.05.1402';
            var checkresult = await setNet.get_New_Version();
            if (checkresult != undefined && checkresult == nowVersion) {
                this.setState({ wait_state: false });
                Alert.alert("版本提示", "当前已是最新版本");
            } else {
                this.setState({ wait_state: false });
                if (checkresult.model) {
                    alert(URL.Version);
                }

            }
        } catch (error) {
            console.log(error);
        }
        }
    }
    
    render() {
        const { navigate } = this.props.navigation;
        return (
            <Container>
                <Modal
                    transparent={true}
                    visible={this.state.wait_state}
                    onRequestClose={() => { }}
                >
                    <Content style={{
                        backgroundColor: 'rgba(0, 0, 0, 0.5)'
                    }}
                        contentContainerStyle={{
                            flex: 1,
                            flexDirection: 'row',
                            alignItems: 'center',
                            justifyContent: 'center'
                        }}>
                        <Spinner style={{ alignSelf: 'center', alignItems: 'center' }} />
                    </Content>
                </Modal>
                <Content style={{ flexDirection: 'column',flex:1}}>
                    <View style={StyleSheet.flatten(styles.view)}>
                        <List>
                            <ListItem onPress={() => { navigate('ModifyPassword') }} style={StyleSheet.flatten(styles.ListItem2)} >
                                <Image style={StyleSheet.flatten(styles.Icon)} source={require('../../images/setting_modfiypas.png')} />
                                <Body>
                                    <Text style={StyleSheet.flatten(styles.text_style)}>修改密码</Text>
                                </Body>
                            </ListItem>
                            <ListItem onPress={() => { this.checkNewVersion() }} style={StyleSheet.flatten(styles.ListItem2)}>
                                <Image style={StyleSheet.flatten(styles.Icon)} source={require('../../images/settting_update.png')} />
                                <Body>
                                    <Text style={StyleSheet.flatten(styles.text_style)}>检查更新</Text>
                                </Body>
                            </ListItem>
                        </List>

                    </View>
                    <View style={{flex:1,flexDirection:'row',justifyContent:'center'}}>
                        <Text style={StyleSheet.flatten(styles.text_style2)}>当前版本号：V1.0</Text>
                    </View>
                </Content>
            </Container>
        );
    }
}
const styles = StyleSheet.create({
    exitButton: {
        marginTop: 15,
        width: width,
        backgroundColor: '#ffffff',
        alignSelf: 'center',
    },
    exittext: {
        alignItems: 'center',
        alignSelf: 'center',
        color: '#000000',
        textAlignVertical: 'center',
    },
    text_style: {
        //fontFamily: '微软雅黑',
        fontSize: 15,
        color: "#000"

    },
    text_style2: {
        //fontFamily: '微软雅黑',
        fontSize: 15,
        color: "#000",
        marginTop:320
    },
    Icon: {
        width: 20,
        height: 20,
        marginLeft: 10,
        marginRight: 10,

    },
    ListItem2: {
        flexDirection: 'row',
        justifyContent: 'center',
        marginRight: 0,
        marginLeft: 0,
        paddingLeft: 10,
        alignItems: 'center',
        backgroundColor: 'white',
        borderBottomWidth: 0.3,
        borderBottomColor: "#cac0c0",
        borderTopColor: "#cac0c0",

    },
    Exit: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        marginRight: 0,
        marginLeft: 0,
        marginTop: 20,
        paddingLeft: 10,
        backgroundColor: 'white',
    },
    view: {
        marginTop: 15,
    },
});


